<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Skills
 *
 * @author Willie
 */
class Trade extends CI_Model{
    //put your code here
    var $teachId='';
    var $learnId='';
    var $want='';
    var $teach='';
    var $user_login_id;
    var $expiration_date;
    //var $sql_get_learnname;

    function __construct() {
        parent::__construct();
    }
    public function initialise($teachId,$learnId){
        $this->learnId=$learnId;
        $this->teachId=$teachId;
    }
    
    public function inputTrades($teachName, $wantName, $expiration_DT, $user_login_id){
        //$this -> input -> post("want",TRUE);
        //$this-> input -> post("teach",TRUE);
        $this->teach=$teachName;
        $this->want=$wantName;
        $this->user_login_id=$user_login_id;
        $this->expiration_date=$expiration_DT;
        
    }


    public function getTrades(){
        $sql="SELECT user_login.id AS uid, matches.DESCRIPTION,matches.TO_LEARN_SKILL_ID,matches.TO_TEACH_SKILL_ID,matches.EXPIRATION_DT,"
              ."master_skill.SKILL_NAME,master_skill.SKILL_TYPE,master_skill.SKILL_DESCRIPTION,user_login.FST_NAME,"
              ."user_login.LAST_NAME FROM matches LEFT JOIN master_skill ON "
              ."matches.TO_TEACH_SKILL_ID=master_skill.id LEFT JOIN user_login ON user_login.id=matches.USER_LOGIN_ID"
              ." where matches.TO_TEACH_SKILL_ID=".$this->learnId;
        
        $results=$this->db->query($sql);
        return $results;
    }
    
    public function get_current_trades(){
        $uid = $this->session->userdata("user_id");
        $sql = "SELECT * FROM matches WHERE USER_LOGIN_ID=".$uid;
        $result = $this->db->query($sql);
        return $result;
    }

    public function getlearnid($uid){
        $uid = $this->session->userdata("user_id");
        if($uid!=NULL){
            $sql = "SELECT TO_LEARN_SKILL_ID FROM matches WHERE USER_LOGIN_ID =".$uid;
            $result = $this->db->query($sql);
            return $result;
        }
    }
    
    public function getteachid($uid){
        $uid = $this->session->userdata("user_id");
        if($uid!=NULL){
            $sql = "SELECT TO_TEACH_SKILL_ID FROM matches WHERE USER_LOGIN_ID =".$uid;
            $result = $this->db->query($sql);
            return $result;
        }
    }
        
         public function getlearnskillbyid($learnid){
            $uid = $this->session->userdata("user_id");
            if ($uid!=NULL)
            {
                $sql = "SELECT SKILL_NAME FROM master_skill where USER_LOGIN_ID =". $uid." AND master_skill.id =".  $this->learnId;
                $affectedRow = $this->db->affected_rows();
                if($affectedRow >0){
                    // meaning some row has been affected
                    echo json_encode(array("success"=>TRUE));
                }else{
                    echo json_encode(array("success"=>FALSE));
                }
            }
        }
        
        public function getteachskillbyid($teachid){
            $uid = $this->session->userdata("user_id");
            if ($uid!=NULL)
            {
                $sql = "SELECT SKILL_NAME FROM master_skill where USER_LOGIN_ID =".$uid." AND master_skill.id =". $this->teachId;
                $this->db->query($sql);
                $affectedRow = $this->db->affected_rows();
                if($affectedRow >0){
                    // meaning some row has been affected
                    echo json_encode(array("success"=>TRUE));
                }else{
                    echo json_encode(array("success"=>FALSE));
                }
            }
        }
        
         public function deleteTrade(){
            $uid = $uid = $this->session->userdata("user_id");
            if($uid!=NULL){
                $table=filter_input(INPUT_POST,'table',FILTER_SANITIZE_STRING);
                $teachid=  filter_input(INPUT_POST,'teachid',FILTER_SANITIZE_STRING);
                $learnid=  filter_input(INPUT_POST,'learnid',FILTER_SANITIZE_STRING);
                //$epid=  filter_input(INPUT_POST,'epid',FILTER_SANITIZE_STRING);
                $sql="DELETE FROM $table WHERE USER_LOGIN_ID =".$uid." AND TO_LEARN_SKILL_ID=".$learnid." AND TO_TEACH_SKILL_ID=".$teachid;//." AND EXPIRATION_DT=".$epid;
                echo $uid, $learnid, $teachid;
                $resp=$this->db->simple_query($sql);
                echo json_encode(array('success'=>$resp));
        }
    }
    
}
